package com.cx.base.day13_io.io_04_convertStream;

import java.io.*;

/**
 * @Author: robert.song
 * @Date: Created in 2022/4/22
 */
public class ConvertStream {

    public static void main(String[] args) throws IOException {
        convertStream();
        convertStreamBuffer();
    }

    public static void convertStream() throws IOException {
        File file1 = new File("." + System.getProperty("file.separator") + "test0.txt");
        File file2 = new File("." + System.getProperty("file.separator") + "test7.txt");
        InputStreamReader isr = new InputStreamReader(new FileInputStream(file1), "utf-8");
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file2), "utf-8");
        int unicodeOfChar = 0;
        try {
            while ((unicodeOfChar = isr.read()) != -1) {
                osw.write(unicodeOfChar);
                osw.flush();
            }
        } finally {
            if (isr != null) {
                isr.close();
            }
            if (osw != null) {
                osw.close();
            }
        }

    }

    public static void convertStreamBuffer() throws IOException {
        File file1 = new File("." + System.getProperty("file.separator") + "test0.txt");
        File file2 = new File("." + System.getProperty("file.separator") + "test7.txt");
        InputStreamReader isr = new InputStreamReader(new FileInputStream(file1), "utf-8");
        OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(file2), "utf-8");
        BufferedReader br = new BufferedReader(isr);
        BufferedWriter bw = new BufferedWriter(osw);
        String readLine = null;
        try {
            while ((readLine = br.readLine()) != null) {
                bw.write(readLine);
            }
        } finally {
            if (br != null) {
                br.close();
            }
            if (bw != null) {
                bw.close();
            }
        }
    }
}
